//index.js 
//获取应用实例 
var app = getApp()
const api = require('../../utils/api.js');
const util = require('../../utils/util.js');
Page({
	data: {
		stars: [0, 1, 2, 3, 4],
		normalSrc: '../../images/rating/unstar.png',
		selectedSrc: '../../images/rating/star.png',
		key: 0, //评分
		content: "",
		// tab切换 
		currentTab: 0,
		showShadow: false,
		kcs: [],
		recommendList: [],
		windowWidth: app.systemInfo.windowWidth,
		windowHeight: app.systemInfo.windowHeight,
		pixelRatio: app.systemInfo.pixelRatio,
		ownerId: "",
		ownerName: "",
	},
	changeColor: function(e) {
		var key = e.currentTarget.dataset.key
		console.log("得" + key + "分")
		this.setData({
			key: key
		})
	},
	onLoad: function() {
		wx.showShareMenu({
			withShareTicket: true
		});
	},
	onShow:function(){
		this.listByTime("0");
	},
	onPullDownRefresh:function(){
		this.listByTime("0");
		wx.stopPullDownRefresh();
	},
	commentsInput: function(e) {
		this.setData({
			content: e.detail.value
		})
	},
	//立即付款
	pay: function(e){
		wx.navigateTo({
			url: '../order/toOrder?id='+e.currentTarget.dataset.id
		})
	},
	toRefund:function(e){
		wx.navigateTo({
			url: '../order/refund?id='+e.currentTarget.dataset.id
		})
	},
	listByTime: function(isOver) {
		let _this = this;
		api.listByTime({
			data: {
				isOver: isOver,
				lng: app.globalData.longitude,
				lat: app.globalData.latitude,
				pageSize: "20",
				pageNum: "1",
				parentsId:app.globalData.userId
			},
			success: function(res) {
				res.data.obj.rows.forEach(function(item) {
					if(item.course.courseImgs != null) {
						item.course.courseImgs = item.course.courseImgs.split(",")[0];
					}
					if(item.course.enterStartTime != null){
						item.course.enterStartTime = util.formatEndTime(item.course.enterStartTime);
					}else{
						if(item.courseTime.startTime != null) {
							item.course.enterStartTime = util.formatEndTime(item.courseTime.startTime);
						}
					}
					if(item.courseTime.startTime != null) {
						item.courseTime.startTime = util.formatHour(new Date(item.courseTime.startTime));
					}
					if(item.orderStatus == "0") {
						item.orderStatusName = "待支付";
					} else if(item.orderStatus == "1") {
						item.orderStatusName = "已支付";
					} else if(item.orderStatus == "2") {
						item.orderStatusName = "已取消";
					}else if(item.orderStatus == "-1") {
						item.orderStatusName = "已退款";
					}else {
						item.orderStatusName = "已核销";
					}
					if(item.hasComment == "0") {
						item.hasCommentName = "未评论";
					}else{
						item.hasCommentName = "已评论";
					}
				});
				_this.recommendList(res.data.obj.rows[0].courseTime.id);
				if(res.data.code == 0) {
					_this.setData({
						kcs: res.data.obj.rows,
						currentTab:Number(isOver),
					});
				}
			},
		});
	},
	// 点击tab切换 
	swichNav: function(e) {
		var that = this;
		if(e.target.dataset.current == "0") {
			console.log(e.target.dataset.current)
			this.listByTime("0");
		} else {
			this.listByTime("1");
			
		}
		that.setData({
			currentTab: e.target.dataset.current
		})
	},
	getLocationMap: function(e) {
		console.log(e);
		wx.openLocation({
			latitude: e.currentTarget.dataset.lat,
			longitude: e.currentTarget.dataset.lng,
			name: e.currentTarget.dataset.name,
			scale: 18
		})
	},
	saveCommenting: function(e) {
		let _this = this;
		api.commentsSave({
			data: {
				content: this.data.content,
				ownerId: this.data.ownerId,
				created: app.globalData.userId,
				nickName: app.globalData.userInfo.nickName,
				score: this.data.key
			},
			success: function(res) {
				_this.setData({
					showShadow: false,
				});
			}
		});
	},
	addComment: function(e) {
		this.setData({
			showShadow: true,
			ownerId: e.currentTarget.dataset.own,
			ownerName: e.currentTarget.dataset.ownername,
			key: 0, //评分
		});
	},
	exit: function() {
		this.setData({
			showShadow: false,
		});
	},
	recommendList: function(id) {
		let _this = this;
		api.recommendList({
			data: {
				id: id,
				lng: app.globalData.longitude,
				lat: app.globalData.latitude,
				pageSize: "10",
				pageNum: "1",
			},
			success: function(res) {
				if(res.data.code == 0) {
					res.data.list.rows.forEach(function(item){
						if(item.enterStartTime != null){
							item.enterStartTime = util.formatEndTime(new Date(item.enterStartTime));
						}else{
							if(item.subTime != null) {
								item.enterStartTime = util.formatEndTime(new Date(item.subTime));
							}
						}
					});
					_this.setData({
						recommendList: res.data.list.rows,
					});
				}
			},
		});
	},
	onShareAppMessage: function(res) {
		if(res.from === 'button') {
			// 来自页面内转发按钮
			console.log(res.target)
		}
		return {
			title: '6KID 遛娃神器发红包啦',
			path: "pages/fetchred/robRed?shareUserId=" + app.globalData.userId,
			imageUrl: "../../images/bigRed.png",
			success: function(res) {
				// 转发成功
			},
			fail: function(res) {
				// 转发失败
			}
		}
	}
})